var app = getApp()
Page({
  data: {
    // 获取原密码值
    userymm: '',
    //  获取新密码值
    userxmm: '',
    // 获取确认密码值
    usersmm: "",
    // 更改原密码值
    ypwd: "",
    // 更改新密码值
    xpwd: "",
    // 更改确认密码值
    qpwd: "",
    // button属性
    wdisabled: false,
    modalHidden: false,
    // Icon样式
    aIcon: false,
    bIcon: false,
    cIcon: false,
  },
  onLoad: function () {
    this.inputNull()
  },
  // 获取原密码值
  userNameInputy: function (e) {
    this.setData({
      ypwd: e.detail.value
    })
    this.inputNull()
  },
  // 获取新密码值
  userNameInputx: function (e) {
    this.setData({
      xpwd: e.detail.value
    })
    this.inputNull()
  },
  // 获取确认密码值
  passWdInput: function (e) {
    this.setData({
      qpwd: e.detail.value
    })
    this.inputNull()
  },

  inputNull: function () {
    if (this.data.ypwd == "" || this.data.xpwd == "" || this.data.qpwd == "") {
      this.setData({
        wdisabled: true
      })
    } else {
      this.setData({
        wdisabled: false
      })
    }
  },

  // 新密码失去焦点验证
  xmminputblur: function (e) {
    var _that = this
    var xmm = _that.data.xpwd
    var xmmphone = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/
    if (xmmphone.test(xmm) == false) {
      var _that = this
      _that.setData(
        { popErrorMsg: "密码必须为8-20位，包含字母及数字", bIcon: true },
      );
      _that.ohShitfadeOut();
      return
    } else {
      _that.setData(
        { popErrorMsg: "", bIcon: false }
      );
    }
  },
  // 确认密码失去焦点验证
  sureinputblur: function (e) {
    var _that = this
    var xmm = _that.data.xpwd
    var smm = _that.data.qpwd
    if (xmm != smm) {
      _that.setData(
        { popErrorMsg: "与新密码不一致", cIcon: true, }
      );
      _that.ohShitfadeOut();
      return
    } else {
      _that.setData(
        { popErrorMsg: "", cIcon: false },
      );
    }
  },

  setDisabled: function () {
    wx.navigateBack();
  }, 
  // 按钮确定事件
  loginBtnClick: function (e) {
    var _that = this
    console.log("原密码：" + this.data.ypwd + " 新密码：" + this.data.xpwd + " 确认密码" + this.data.qpwd);
    var ymm = _that.data.ypwd
    var xmm = _that.data.xpwd
    var smm = _that.data.qpwd
    var xmmphone = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/
    // 判断原密码是否否和正则验证
    if (ymm.length < 6 || ymm.length > 32) {
      var _that = this
      _that.setData(
        { popErrorMsg: "原密码不正确", aIcon: true },
      );
      _that.ohShitfadeOut();
      return
    } else {
      _that.setData(
        { popErrorMsg: "", aIcon: false }
      );
      // 判断新密码是否否和正则验证
      if (xmmphone.test(xmm) == false) {
        var _that = this
        _that.setData(
          { popErrorMsg: "密码必须为8-20位，包含字母及数字", bIcon: true },
        );
        _that.ohShitfadeOut();
        return
      } else {
        _that.setData(
          { popErrorMsg: "", bIcon: false }
        );
        // 判断新密码与确认密码是否相同
        if (xmm != smm) {
          _that.setData(
            { popErrorMsg: "与新密码不一致", cIcon: true, }
          );
          _that.ohShitfadeOut();
          return
        } else {
          _that.setData(
            { popErrorMsg: "", cIcon: false },
          );
          // 判断新密码与确认密码是否相同
          if (ymm == smm) {
            _that.setData(
              { popErrorMsg: "原密码与新密码一致", bIcon: true, }
            );
            _that.ohShitfadeOut();
            return
          } else {
            _that.setData(
              { popErrorMsg: "", bIcon: false },
            );
            // 调用接口
            app.request({
              url: app.globalData.apiUrl +'/mobileOfficing/api/modifyPassword?oldPsw=' + ymm + '&newPsw=' + xmm,
              method: "POST",
              data: {
              },
              header: {
                'header': 'Content-Type: application/json',
                'sessionId': wx.getStorageSync(app.globalData.sessionId)
              },
              success: function (res) {
                console.log(res.data.msg)
                if (res.data.result) {
                  wx.showToast({
                    title: '修改成功',
                    icon: 'succes',
                    duration: 5000,
                    mask: true,
                    complete: function () {
                      wx.navigateTo({
                        url: '../personalCenter/personalCenter'
                      })
                      this.setData({
                        modalHidden: !this.data.modalHidden,
                      });
                      wx.removeStorageSync('sessionKey');
                      wx.navigateTo({
                        url: '../logIn/logIn'
                      })
                    },
                  })
                } else {
                  _that.setData(
                    { popErrorMsg: res.data.msg }
                  );
                  _that.ohShitfadeOut();
                }
              }
            })
          }
        }
      }
    }
  },
  // 弹窗关闭
  ohShitfadeOut() {
    var fadeOutTimeout = setTimeout(() => {
      this.setData({ popErrorMsg: '' });
      clearTimeout(fadeOutTimeout);
    }, 3000);
  },
})